# coding: utf8

#########################################################################
## Customize your APP title, subtitle and menus here
#########################################################################

response.title = request.application
response.subtitle = '' #T('customize me!')

##########################################
## this is the authentication menu
## remove if not necessary
##########################################

if 'auth' in globals():
    if not auth.is_logged_in():
       response.menu_auth = [
           [T('Login'), False, auth.settings.login_url,
            [
                   [T('Register'), False,
                    URL(request.application,'default','user/register')],
                   [T('Lost Password'), False,
                    URL(request.application,'default','user/retrieve_password')]]
            ],
           ]
    else:
        response.menu_auth = [
            ['User: '+auth.user.first_name,False,None,
             [
                    [T('Logout'), False, 
                     URL(request.application,'default','user/logout')],
                    [T('Edit Profile'), False, 
                     URL(request.application,'default','user/profile')],
                    [T('Change Password'), False,
                     URL(request.application,'default','user/change_password')]]
             ],
            ]

##########################################
## this is the main application menu
## add/remove items as required
##########################################

response.menu = [
   (T('About'), False, "http://code.google.com/p/code-by-example/"),
]


    
##########################################
## this is here to provide shortcuts
## during development. remove in production 
##########################################

if not auth.has_membership(auth.user_id, role='admin'): 
    response.menu.append(
      ( T('DB'), False,  URL(request.application, 'appadmin', 'index')
       )
    )
#~ if True:
else:
    response.menu+=[
        (T('Edit'), False, URL('admin', 'default', 'design/%s' % request.application),
         [
                (T('Controller'), False, 
                 URL('admin', 'default', 'edit/%s/controllers/%s.py' \
                         % (request.application,request.controller=='appadmin' and
                            'default' or request.controller))), 
                (T('View'), False, 
                 URL('admin', 'default', 'edit/%s/views/%s' \
                         % (request.application,response.view))),
                (T('Layout'), False, 
                 URL('admin', 'default', 'edit/%s/views/layout.html' \
                         % request.application)),
                (T('Stylesheet'), False, 
                 URL('admin', 'default', 'edit/%s/static/base.css' \
                         % request.application)),
                (T('DB Model'), False, 
                 URL('admin', 'default', 'edit/%s/models/db.py' \
                         % request.application)),
                (T('Menu Model'), False, 
                 URL('admin', 'default', 'edit/%s/models/menu.py' \
                         % request.application)),
                (T('Database'), False, 
                 URL(request.application, 'appadmin', 'index')),
                ]
       ) ,
      ]


    
##########################################
## 
## user preferences stuff -- U
## 
##########################################

class USER_PREFERENCES(MENU):
    """
    Used to build menus for USER_PREFERENCES
    SUPERFISH hijacks UL/LI elements even if I don't want it
    might be replaced by some of those:
    http://docs.jquery.com/UI/API/1.7.2/Draggable
    http://docs.jquery.com/UI/API/1.7.2/Draggable#option-cursorAt

    http://stackoverflow.com/questions/1328249/remembering-a-jquery-draggable-tool-palette-position-between-page-refreshes/1328311#1328311

    ?? http://fromvega.com/wordpress/2007/07/14/easydrag-jquery-plugin/
    http://pop.seaofclouds.com/
    http://www.sitepoint.com/blogs/2009/03/31/make-a-mega-drop-down-menu-with-jquery/
    http://labs.makotokw.com/en/jquery/ui_potato_menu
    http://code.google.com/p/dropdown-check-list/
    many more at http://www.1stwebdesigner.com/resources/38-jquery-and-css-drop-down-multi-level-menu-solutions/
    
    Optional arguments
      _class: defaults to 'web2py-menu web2py-menu-vertical'
      ul_class: defaults to 'web2py-menu-vertical'
      li_class: defaults to 'web2py-menu-expand'

    Example:
        user_prefs_menu = USER_PREFERENCES([['name', CONTENT, [submenu]], ...])
        {{=user_prefs_menu}}
    """

    tag = 'ul'

    def __init__(self, data, **args):
        self.data = data
        self.attributes = args
        if not '_class' in self.attributes:
            self['_class'] = 'web2py-menu web2py-menu-vertical'
        if not 'ul_class' in self.attributes:
            self['ul_class'] = 'web2py-menu-vertical'
        if not 'li_class' in self.attributes:
            self['li_class'] = 'web2py-menu-expand'

    def serialize(self, data, level=0):
        if level == 0:
            ul = UL(**self.attributes)
        else:
            ul = UL(_class=self['ul_class'])
            
        if isinstance(data, (list, tuple) ) :  # something very strange, why it wants to consume strings and so on...?   
            for item in data:
                content  = item[0]
                li = LI(  content )
                
                if (len(item) > 1) and item[1]:
                    inner_items = item[1]
                    li = LI( A( content, _href='#null'),
                             self.serialize(inner_items, level+1)
                         )
                    #~ li['_class'] = self['li_class']
                ul.append(li)
            
        else :
            ul.append( LI(  data ) )
                
            #~ if len(item) == 1:
                #~ li = LI(  content )
            #~ elif item[1]:
                #~ inner_items = item[1]
                #~ li = LI(  A(content, _href='#null'),
                          #~ self.serialize(inner_items, level+1)
                       #~ )
                #~ li['_class'] = self['li_class']
                
            
        return ul

    def xml(self):
        return self.serialize(self.data, 0).xml()
#~ 
#~ ##########################################
#~ 
response.user_prefs_menu = [ ]
#~ response.user_prefs_menu = [ 
        #~ (  T('User Preferences'), None,
              #~ [

                  #~ ( T('Topics Menu'), None,
                    #~ [( 'Nieko',
                        #~ LOAD(c=request.controller, f='topics_tree_view', ajax=False, ajax_trap=False),
                     #~ )
                    #~ ] 
                  #~ )  , 
#~ 
                #~ (   T('Vars'), 
                    #~ None #~ BEAUTIFY( request )
                #~ )  
#~ 
              #~ ]
        #~ ),
#~ 
   #~ ]     
